<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Admin\Model\AuthGroupModel;
use Think\Page;

require_once './lib/function.inc.php';
require_once './lib/AopSdk.php';

/**
 * 后台报名信息管理控制器
 * @author huajie <banhuajie@163.com>
 */
class RedEnvelopesController extends AdminController {



    /**红包管理
     */
    public function index(){

        $Model   =   M('red_envelopes');
        $list = $this->lists($Model,array(),'id desc',array(),true,50);

        $this->assign('_list', $list);
        $this->meta_title = '红包列表';
        $this->display();
    }


    public function add(){
        $this->meta_title = '添加图片信息';
        $this->display('edit');
}



    //编辑
    public function edit(){
        $id=I('get.id');
        $data=M('red_envelopes')->where(array('id'=>$id))->find();
        $this->assign('info',$data);
        $this->meta_title = '编辑信息';
        $this->display('edit');
    }


    //保存
    public function saveRedPackge(){
        $data=I('post.');
        if(empty($data)){
            $this->error ("请填写完整数据");
        }

        if(empty($data['id'])){ //新增数据
            $data['create_time']=time();
            $id    = M('red_envelopes')->add($data);
            if(!$id){
                $this->error ("新增出错");
            }else{
                $this->success ('新增成功！', U('index'));
            }
        } else { //更新数据
            $status = M('red_envelopes')->save($data);
            if(false === $status){
                $this->error ("更新出错");
            }else{
                $this->success ('更新成功！', U('index') );
            }
        }
    }

    public function create($data){
        $config=C('ALI');
        $aop = new \AopClient ();
        $aop->gatewayUrl = $config['gatewayUrl'];
        $aop->appId = $config['app_id'];
        $aop->rsaPrivateKey = $config['merchant_private_key'];
        $aop->alipayrsaPublicKey=$config['alipay_public_key'];
        $aop->apiVersion = '1.0';
        $aop->signType = $config['sign_type'];
        $aop->postCharset='utf-8';
        $aop->format='json';
        $request = new AlipayMarketingCampaignCashCreateRequest ();
        $request->setBizContent("{" .
            "\"coupon_name\":\"{$data['name']}\"," .
            "\"prize_type\":\"random\"," .
            "\"total_money\":\"{$data['money']}\"," .
            "\"total_num\":\"{$data['num']}\"," .
            "\"prize_msg\":\"送您一个红包\"," .
            "\"start_time\":\"{$data['start_time']}\"," .
            "\"end_time\":\"{$data['end_time']}\"," .
            "\"merchant_link\":\"".$config['merchant_link']."\"," . // 这里不要删掉用于支付成功后触发红包
            "\"send_freqency\":\"D1|L7\"" .
            "  }");
        $result = $aop->execute ( $request);
        writeLog ( "result: " . var_export ( $result, true ) );
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        $arr=object_array($result->$responseNode);
        echo json_encode($arr);
        if(!empty($resultCode)&&$resultCode == 10000){
            printf("crowd_no:%s\r\norigin_crowd_no:%s\r\npay_url:%s",$arr["crowd_no"],$arr["origin_crowd_no"],urldecode($arr["pay_url"]));
        }
    }



    public function delete(){
        $id = array_unique((array)I('ids',0));
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] =   array('in',$id);
        $re = M('red_envelopes')->where($map)->delete();
        if($re!==false){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    }



}
